/**
 * 
 */
package org.compiere.model;

import java.sql.ResultSet;
import java.util.List;
import java.util.Properties;

import org.compiere.util.Env;

/**
 * @author V.Sokolov
 *
 */
public class MBPMForm extends X_BPM_Form {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5236262513637596250L;

	/**
	 * @param ctx
	 */
	public MBPMForm(Properties ctx) {
		super(ctx);
	}

	/**
	 * @param ctx
	 * @param BPM_Form_ID
	 * @param trxName
	 */
	public MBPMForm(Properties ctx, int BPM_Form_ID, String trxName) {
		super(ctx, BPM_Form_ID, trxName);
	}

	/**
	 * @param ctx
	 * @param rs
	 * @param trxName
	 */
	public MBPMForm(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}
	
	public static MBPMFormLine[] getLines(Properties ctx, int BPM_Form_ID, String trxName){
		
		List<MBPMFormLine> list = new Query(ctx, I_BPM_FormLine.Table_Name, "BPM_Form_ID=?", trxName)
		.setParameters(BPM_Form_ID).setOrderBy(I_BPM_FormLine.COLUMNNAME_LineNo)
		.setOnlyActiveRecords(true)
		.list();
		
		MBPMFormLine[] retValue = new MBPMFormLine[list.size ()];
		
		list.toArray (retValue);
		
		return retValue;
		
	}
	
	private MBPMFormLine[] m_lines = null;
	
	public MBPMFormLine[] getLines(boolean requery){
		
		if (m_lines != null && !requery) {
			set_TrxName(m_lines, get_TrxName());
			return m_lines;
		}
		List<MBPMFormLine> list = new Query(Env.getCtx(), I_BPM_FormLine.Table_Name, "BPM_Form_ID=?", get_TrxName())
											.setParameters(get_ID()).setOrderBy(I_BPM_FormLine.COLUMNNAME_LineNo)
											.setOnlyActiveRecords(true)
											.list();
		
		m_lines = list.toArray(new MBPMFormLine[list.size ()]);

		return m_lines;	
	}

	public static MBPMForm[] getForms(int BPM_VersionBudget_ID, int AD_Org_ID){
	  
	  List<MBPMForm> list =null;	
	  if ((AD_Org_ID>0)&&(AD_Org_ID!=1000007)) { 
		  	list =  new Query(Env.getCtx(), I_BPM_Form.Table_Name, "BPM_VersionBudget_ID = ? AND (AD_Org_ID=? OR AD_Org_ID=?)", null)
		  	.setParameters(BPM_VersionBudget_ID, AD_Org_ID, 0)
		  	.setOnlyActiveRecords(true)
		  	.setOrderBy(COLUMNNAME_BPM_Form_ID)
		  	.list();
	  } else { 	
			list = new Query(Env.getCtx(), I_BPM_Form.Table_Name, "BPM_VersionBudget_ID = ? AND AD_Org_ID=0 AND Name LIKE '%БО%' ", null)
			.setParameters(BPM_VersionBudget_ID)
			.setOnlyActiveRecords(true)
			.setOrderBy(COLUMNNAME_BPM_Form_ID)
			.list();
	  }		
	  MBPMForm[] retValue = new MBPMForm[list.size ()];
	  list.toArray(retValue);
	  return retValue;
		
	}
	
	public static MBPMForm[] getForms(int BPM_VersionBudget_ID, int AD_Org_ID, String CategoryName){
		  
		  List<MBPMForm> list =null;	
		  if ((AD_Org_ID>0)&&(AD_Org_ID!=1000007)) { 
			  	list =  new Query(Env.getCtx(), I_BPM_Form.Table_Name, "BPM_VersionBudget_ID = ? " +
			  			"AND (AD_Org_ID=? OR AD_Org_ID=?) AND BPM_FormOrderBy = '"+CategoryName+"'", null)
			  	.setParameters(BPM_VersionBudget_ID, AD_Org_ID, 0)
			  	.setOnlyActiveRecords(true)
			  	.setOrderBy(COLUMNNAME_BPM_Form_ID)
			  	.list();
		  } else { 	
				list = new Query(Env.getCtx(), I_BPM_Form.Table_Name, "BPM_VersionBudget_ID = ? " +
						"AND AD_Org_ID=0 AND Name LIKE '%БО%' AND BPM_FormOrderBy = '"+CategoryName+"'", null)
				.setParameters(BPM_VersionBudget_ID)
				.setOnlyActiveRecords(true)
				.setOrderBy(COLUMNNAME_BPM_Form_ID)
				.list();
		  }		
		  MBPMForm[] retValue = new MBPMForm[list.size ()];
		  list.toArray(retValue);
		  return retValue;
			
		}


}
